import pandas as pd
import numpy as np

n_clusters = 62
data = pd.read_csv("data_model.csv")
label_5 = data['Label'].values
gmm_label = pd.read_csv("gmm_label.csv").values.ravel()

conn = [[0 for _ in range(5)] for _ in range(n_clusters)]
for i in range(len(label_5)):
	conn[gmm_label[i]][label_5[i]-1] += 1
'''
#analyse the connections between GMM labels and origin labels
temp = [0 for _ in range(n_clusters)]
ratio = [0 for _ in range(n_clusters)]
ana = [0,0,0,0,0,0,0]
total = []
for i in range(n_clusters):
	for j in range(5):
		if conn[i][j] > conn[i][temp[i]]:
			temp[i] = j
	s = sum(conn[i])
	if s > 20:
		total.append(i)
	ratio[i] = round(conn[i][temp[i]] / s,4)
	temp[i] += 1
	if ratio[i] == 1:
		ana[0] +=1

	elif ratio[i] < 1 and ratio[i] >= 0.9:
		ana[1] +=1

	elif ratio[i] < 0.9 and ratio[i] >= 0.8:
		ana[2] +=1

	elif ratio[i] < 0.8 and ratio[i] >= 0.7:
		ana[3] +=1

	elif ratio[i] < 0.7 and ratio[i] >= 0.6:
		ana[4] +=1

	elif ratio[i] < 0.6 and ratio[i] >= 0.5:
		ana[5] +=1

	else:
		ana[6] +=1

print(ana)
print(len(total))

#np.savetxt( "conn.csv",conn,delimiter=",",fmt = '%d')
#np.savetxt( "temp.csv",temp,delimiter=",",fmt = '%d')
#np.savetxt( "ratio.csv",ratio,delimiter=",",fmt = '%.4f')
'''
for i in range(n_clusters):
	for x in range(len(gmm_label)):
		if gmm_label[x] == i:
			if i in total:
				gmm_label[x] = temp[i]
			else:
				gmm_label[x] = 6
labels = pd.DataFrame(data = gmm_label,columns = ['New Label'])
labels.to_csv('new_label.csv', index = False)
